﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;

public partial class Admin_KhachHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    EDiamondEntities1 db = new EDiamondEntities1();
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int khoa = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        try
        {
            KhachHang entity = db.KhachHangs.Include("DatHangs").SingleOrDefault(p => p.KhachHangID == khoa);

            if (entity.DatHangs.Count > 0)
            {
                throw new Exception("Không hủy được ");
                
            }
            db.KhachHangs.DeleteObject(entity);
            db.SaveChanges();
            throw new Exception("hủy được ");

        }
        catch (Exception ex)
        {
            throw new Exception("Không hủy được ");
        }



        //int khoa = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        //try
        //{
        //    KhachHang entity = db.KhachHangs.FirstOrDefault(p => p.KhachHangID == khoa);

        //    db.KhachHangs.DeleteObject(entity);
        //    db.SaveChanges();
        //    Response.Write("<script> alert('Hủy thành công !'); </script>");
        //}
        //catch (Exception ex)
        //{
        //    Response.Write("<script> alert('Hủy không thành công !'); </script>");
        //}
    }
}